<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>cannon.js - single body on plane demo</title>
    <link rel="stylesheet" href="css/style.css" type="text/css" />
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
  </head>
  <body>
    <script type="module">
      import * as CANNON from '../dist/cannon-es.js'
      import { Demo } from './js/Demo.js'

      /**
       * Just a simple body on a plane.
       */

      const demo = new Demo()

      demo.addScene('Sphere', () => {
        const world = setupWorld(demo)

        const size = 2

        const sphereShape = new CANNON.Sphere(size)

        const body = new CANNON.Body({ mass: 30 })
        body.addShape(sphereShape)
        body.position.set(0, size * 2, size)
        world.addBody(body)
        demo.addVisual(body)
      })

      demo.addScene('Box', () => {
        const world = setupWorld(demo)

        const size = 2

        const boxShape = new CANNON.Box(new CANNON.Vec3(size, size, size))

        const body = new CANNON.Body({ mass: 30 })
        body.addShape(boxShape)
        body.position.set(0, size * 2, size)
        world.addBody(body)
        demo.addVisual(body)
      })

      demo.start()

      function setupWorld(demo) {
        const world = demo.getWorld()
        world.gravity.set(0, -10, 0)

        // Tweak contact properties.
        // Contact stiffness - use to make softer/harder contacts
        world.defaultContactMaterial.contactEquationStiffness = 1e7

        // Stabilization time in number of timesteps
        world.defaultContactMaterial.contactEquationRelaxation = 4

        // Static ground plane
        const groundShape = new CANNON.Plane()
        const groundBody = new CANNON.Body({ mass: 0 })
        groundBody.addShape(groundShape)
        groundBody.quaternion.setFromEuler(-Math.PI / 2, 0, 0)
        world.addBody(groundBody)
        demo.addVisual(groundBody)

        return world
      }
    </script>
  </body>
</html>
